/*
 * Copyright (C) 2017 ~ 2019 Deepin Technology Co., Ltd.
 *
 * Author:     zhangbingyi <zhangbingyi@uniontech.com>
 *
 * Maintainer: zhangbingyi <zhangbingyi@uniontech.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "datausagemodel.h"
#include "gsettingkey.h"
#include "invokers/invokerfactory.h"
#include "common/systemsettings.h"

#include <QFile>
#include <QProcess>
#include <QLabel>
#include <QMessageBox>
#include <QDateTime>
#include <QCoreApplication>
#include <QDebug>

DataUsageModel::DataUsageModel(QObject *parent)
    : QObject(parent)
    , m_sysSettings(new SystemSettings(this))
    , m_monitorInterFaceServer(nullptr)
    , m_dataInterFaceServer(nullptr)
    , m_gsNetControl(nullptr)
{
    m_monitorInterFaceServer = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.MonitorNetFlow",
                                                                           "/com/deepin/defender/MonitorNetFlow",
                                                                           "com.deepin.defender.MonitorNetFlow",
                                                                           ConnectType::SYSTEM, this);
    m_dataInterFaceServer = InvokerFactory::GetInstance().CreateInvoker("com.deepin.defender.datainterface",
                                                                        "/com/deepin/defender/datainterface",
                                                                        "com.deepin.defender.datainterface",
                                                                        ConnectType::SESSION, this);
    m_gsNetControl = InvokerFactory::GetInstance().CreateSettings("com.deepin.dde.deepin-defender", QByteArray(), this);

    //链接trafficdetails服务发送的所有应用流量数据信号
    registerDefenderProcInfoMetaType();
    registerDefenderProcInfoListMetaType();
    bool connetRst = true;
    connetRst &= m_monitorInterFaceServer->Connect("SendProcInfoList", this, SLOT(recProcInfoList(DefenderProcInfoList)));
    connetRst &= m_monitorInterFaceServer->Connect("SendAllAppFlowList", this, SLOT(recAllppFlowList(int, DefenderProcInfoList)));

    //链接trafficdetails服务发送的应用流量数据信号
    connetRst &= m_monitorInterFaceServer->Connect("SendAppFlowList", this, SLOT(recAppFlowList(QString, int, DefenderProcInfoList)));
}

QString DataUsageModel::GetNetControlAppStatus() const
{
    return m_gsNetControl->GetValue(NET_CONTROL_APPS_STATUS).toString();
}

bool DataUsageModel::GetDataUsageSwitchStatus() const
{
    return m_sysSettings->getValue(DATA_USAGE_ON_OFF).toBool();
}

void DataUsageModel::SetNetAppStatusChange(const QString &pkgName, int index) const
{
    DBUS_NOBLOCK_INVOKE(m_dataInterFaceServer, "setNetAppStatusChange", pkgName, index);
}

void DataUsageModel::AddSecurityLog(const QString &info) const
{
    DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AddSecurityLog", SECURITY_LOG_TYPE_TOOL, info);
}

DefenderProcInfoList DataUsageModel::getProcInfoList() const
{
    return m_procInfoList;
}

void DataUsageModel::asyncGetAppFlowList(const QString &pkgName, const TimeRangeType &timeRangeType)
{
    DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AsyncGetAppFlowList", pkgName, timeRangeType);
}

void DataUsageModel::asyncGetAllAppFlowList(const TimeRangeType &timeRangeType)
{
    DBUS_NOBLOCK_INVOKE(m_monitorInterFaceServer, "AsyncGetAllAppFlowList", timeRangeType);
}

bool DataUsageModel::getUosResourceStatus()
{
    QDBusMessage msg = DBUS_BLOCK_INVOKE(m_dataInterFaceServer, "GetUosResourceStatus");
    GET_MESSAGE_VALUE(bool, status, msg);
    return status;
}

void DataUsageModel::recProcInfoList(const DefenderProcInfoList &procInfos)
{
    m_procInfoList.clear();
    for (DefenderProcInfo info : procInfos) {
        // 获取应用名称
        info.sAppName = getAppNameFromDesktop(info.sDesktopPath);
        m_procInfoList.append(info);
    }
    Q_EMIT sendProcInfoList(m_procInfoList);
}

void DataUsageModel::recAllppFlowList(const int timeRangeType, const DefenderProcInfoList &flowLst)
{
    DefenderProcInfoList flowListTmp;
    for (DefenderProcInfo info : flowLst) {
        // 获取应用名称
        info.sAppName = getAppNameFromDesktop(info.sDesktopPath);
        flowListTmp.append(info);
    }
    Q_EMIT sendAllAppFlowList(timeRangeType, flowListTmp);
}

void DataUsageModel::recAppFlowList(const QString &pkgName, const int timeRangeType, const DefenderProcInfoList &flowLst)
{
    DefenderProcInfoList flowListTmp;
    for (DefenderProcInfo info : flowLst) {
        // 获取应用名称
        info.sAppName = getAppNameFromDesktop(info.sDesktopPath);
        flowListTmp.append(info);
    }
    Q_EMIT sendAppFlowList(pkgName, timeRangeType, flowListTmp);
}

QString DataUsageModel::getAppNameFromDesktop(const QString &desktopPath)
{
    return Utils::getAppNameFromDesktop(desktopPath);
}
